
cc.Class({
    extends: cc.Component,

    properties: {
        scrollView:{
            default: null,
            type: cc.Node
        },
    },

    onLoad () {
        this.topNum = 0;
        this.bottomNum = 0;
        this.scrollViewCom = this.scrollView.getComponent("scrollViewCom");
        this.scrollViewCom.init();

        for(var i = 0; i < 500; i++)
        {
            let data = 
            {
                key:2,
                itemData: 
                {
                    labelNum: i,
                    isSel : false,
                },
            }
            this.scrollViewCom.append_data([data]);
        }
    },

    addBtnClick(event, type){
        if (type == 0){//添加到顶部
            console.log("顶部点了次数"+ this.topNum);
            this.topNum ++;

            let data = 
            {
                key: Math.floor(Math.random() * 4),
                itemData :
                {
                    labelNum : "顶部添加"+this.topNum,
                    isSel : false,
                },
            }
            this.scrollViewCom.insert_data(0, [data]);
        }
        if (type == 1){//添加到底部
            console.log("底部点了次数"+ this.bottomNum);
            this.bottomNum ++;

            var data = 
            {
                key: Math.floor(Math.random() * 4),
                itemData:
                {
                    labelNum: "底部添加"+this.bottomNum,
                    isSel : false,
                }
            }
            this.scrollViewCom.append_data([data]);
            this.scrollViewCom.scroll_to_end();
        }
    },

    onClickItem:function(index)
    {
        if(this._selItem)
        {
            if(this._selItem.data.index == index)
            {
                return;
            }

            this._selItem.data.itemData.isSel = false;
        }

        this._selItem = this.scrollViewCom.get_Item(index);
        this._selItem.data.itemData.isSel = true;
        this.scrollViewCom.Refresh_viewPort();

        //动态增删item
        if(this._selAddItem)
        {
            this.scrollViewCom.remove_data(this._selAddItem.data.index);
        }

        var itemData = 
        {
            labelNum: "底部添加"+this._selItem.data.itemData.labelNum,
        };
        var data = {key: 1, itemData}
        var addItemIndex = this._selItem.data.index + 1
        this.scrollViewCom.insert_data(addItemIndex, [data]);
        this._selAddItem = this.scrollViewCom.get_Item(addItemIndex);
    },
    
});
